package com.tutu.controller.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;
import com.tutu.R;
import com.tutu.constant.LoginConst;
import com.tutu.constant.LogoutConst;
import com.tutu.controller.activity.LoginActivity;
import com.tutu.model.Model;

import static com.superrtc.mediamanager.EMediaManager.initData;

/**
 * @author 图图 2020/10/11
 * 设置的Fragment
 */
public class SettingFragment extends Fragment {

    private Button bt_setting_logout;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        // 加载view
        View view = View.inflate(getActivity(), R.layout.fragment_setting, null);
        //初始化
        initView(view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // 处理业务逻辑
        initData();
    }

    private void initData() {
        // 1.在button上显示当前用户名称
        bt_setting_logout.setText("退出登录("+ EMClient.getInstance().getCurrentUser() +")");
        // 2.退出登录
        bt_setting_logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 关闭DBhelper
                Model.getInstance().getDbManager().close();

                // 1.联网去环信服务器退出
                Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
                    @Override
                    public void run() {
                        // 登录环信服务器进行退出登录
                        EMClient.getInstance().logout(false, new EMCallBack() {
                            // 退出成功时
                            @Override
                            public void onSuccess() {
                                // 回到登录页
                                getActivity().runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        // 提示登录成功，更新UI显示
                                        Toast.makeText(getActivity(), LogoutConst.LOGOUT_SUCCESS,Toast.LENGTH_SHORT).show();

                                        // 回到登录页
                                        openNewPage(getActivity(),LoginActivity.class);
                                    }
                                });

                            }

                            // 退出失败时
                            @Override
                            public void onError(int i, String s) {
                                Toast.makeText(getActivity(), LogoutConst.LOGOUT_ERROR + s,Toast.LENGTH_SHORT).show();
                            }

                            // 正在退出时
                            @Override
                            public void onProgress(int i, String s) {

                            }
                        });
                    }
                });
            }
        });
    }

    /**
     * 初始化控件
     * @param view
     */
    private void initView(View view) {
        bt_setting_logout = (Button)view.findViewById(R.id.bt_setting_logout);
    }


    /**
     * 跳转
     * @param packageContext
     * @param cls
     */
    private void  openNewPage(Context packageContext, Class<?> cls){
        // 页面跳转
        Intent intent =  new Intent(packageContext,cls);
        // 启动跳转
        startActivity(intent);
        // 结束当前页
        getActivity().finish();
    }

}
